/**
 * @file spi.c
 * @brief COSC361 - MCU Jukebox Project\n
 * Group: Hamish Cumming, Brian Thorne\n
 * Latest source can be obtained using SVN from google's code repository with the following command:\n
 * svn checkout http://mega8-hob.googlecode.com/svn/trunk/ jukebox-hamish-brian\n
 * Description: Source file for the SPI module\n
 * Date: 25/5/2008
 */

#include "Board.h"
#include "spi.h"

//The chip select register
#define SPI_CSR0 0x00000F82
//Peripheral Select
#define SPI_PCS 0
//The mode register
#define SPI_MR 0x00000083 
//The High byte of the Transmit Data Register
#define SPI_DR_HI 0x0900

/**
 * @brief Sets up and enables the SPI
 */
inline void SPI_init (void){
    AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, (AT91C_PA11_NPCS0 | AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA14_SPCK), 0);
    AT91F_SPI_CfgPMC ();
    AT91F_SPI_Enable(AT91C_BASE_SPI);
    AT91F_SPI_CfgMode (AT91C_BASE_SPI, SPI_MR);
    AT91F_SPI_CfgCs (AT91C_BASE_SPI, SPI_PCS, SPI_CSR0);
}

/**
 * @brief Transmit data via SPI
 */
inline void SPI_transmitToDAC(unsigned int data){
    AT91F_SPI_PutChar(AT91C_BASE_SPI, (SPI_DR_HI | (data & 0xFFFF)), SPI_PCS);
}
